import json

from asgiref.sync import async_to_sync
from celery.result import AsyncResult
from channels.layers import get_channel_layer

from celery_comsumer.main import app

        
def get_celery_result(id):
    async_result = AsyncResult(id=id,app=app)
    return async_result.status
    # if async_result.successful():
    #     print(async_result.get())
    #     return async_result.get()
    # elif async_result.

def send_group_msg(room_name, message):
    # 从Channels的外部发送消息给Channel
    """
    from assets import consumers
    consumers.send_group_msg('ITNest', {'content': '这台机器硬盘故障了', 'level': 1})
    consumers.send_group_msg('ITNest', {'content': '正在安装系统', 'level': 2})
    :param room_name:
    :param message:
    :return:
    """
    channel_layer = get_channel_layer()
    async_to_sync(channel_layer.group_send)(
        'job_{}'.format(room_name),  # 构造Channels组名称
        {
            "type": "system_message",
            "message": message,
        }
    )





